package com.luo.chengrui.module.sa.controller.admin.matrix;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.matrix.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.matrix.OporgManagerMatrixDO;
import com.luo.chengrui.module.sa.convert.matrix.OporgManagerMatrixConvert;
import com.luo.chengrui.module.sa.service.matrix.OporgManagerMatrixService;

@Tag(name = "管理后台 - 组织管理矩阵")
@RestController
@RequestMapping("/sa/oporg-manager-matrix")
@Validated
public class OporgManagerMatrixController {

    @Resource
    private OporgManagerMatrixService oporgManagerMatrixService;

    @PostMapping("/create")
    @Operation(summary = "创建组织管理矩阵")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix:create')")
    public CommonResult<String> createOporgManagerMatrix(@Valid @RequestBody OporgManagerMatrixCreateReqVO createReqVO) {
        return success(oporgManagerMatrixService.createOporgManagerMatrix(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织管理矩阵")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix:update')")
    public CommonResult<Boolean> updateOporgManagerMatrix(@Valid @RequestBody OporgManagerMatrixUpdateReqVO updateReqVO) {
        oporgManagerMatrixService.updateOporgManagerMatrix(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织管理矩阵")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix:delete')")
    public CommonResult<Boolean> deleteOporgManagerMatrix(@RequestParam("id") String id) {
        oporgManagerMatrixService.deleteOporgManagerMatrix(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织管理矩阵")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix:query')")
    public CommonResult<OporgManagerMatrixRespVO> getOporgManagerMatrix(@RequestParam("id") String id) {
        OporgManagerMatrixDO oporgManagerMatrix = oporgManagerMatrixService.getOporgManagerMatrix(id);
        return success(OporgManagerMatrixConvert.INSTANCE.convert(oporgManagerMatrix));
    }

    @GetMapping("/list")
    @Operation(summary = "获得组织管理矩阵列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix:query')")
    public CommonResult<List<OporgManagerMatrixRespVO>> getOporgManagerMatrixList(@RequestParam("ids") Collection<String> ids) {
        List<OporgManagerMatrixDO> list = oporgManagerMatrixService.getOporgManagerMatrixList(ids);
        return success(OporgManagerMatrixConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织管理矩阵分页")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix:query')")
    public CommonResult<PageResult<OporgManagerMatrixRespVO>> getOporgManagerMatrixPage(@Valid OporgManagerMatrixPageReqVO pageVO) {
        PageResult<OporgManagerMatrixDO> pageResult = oporgManagerMatrixService.getOporgManagerMatrixPage(pageVO);
        return success(OporgManagerMatrixConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织管理矩阵 Excel")
    @PreAuthorize("@ss.hasPermission('sa:oporg-manager-matrix:export')")
    @OperateLog(type = EXPORT)
    public void exportOporgManagerMatrixExcel(@Valid OporgManagerMatrixExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OporgManagerMatrixDO> list = oporgManagerMatrixService.getOporgManagerMatrixList(exportReqVO);
        // 导出 Excel
        List<OporgManagerMatrixExcelVO> datas = OporgManagerMatrixConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "组织管理矩阵.xls", "数据", OporgManagerMatrixExcelVO.class, datas);
    }

}
